package monster.com.cvh.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider;
import android.support.v7.widget.AppCompatImageView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.io.File;

import monster.com.cvh.R;
import monster.com.cvh.activity.base.BaseActivity;
import monster.com.cvh.view.SuperFileView2;

/**
 * @author: 13580
 * @data: 2018/4/10
 * @function:文件预览Activity
 */
public class FileDisplayActivity extends BaseActivity implements View.OnClickListener {
    //FileProvider的author和权限申请code
    private static final String AUTHORITIES = "monster.com.cvh.MyFileProvider";
    SuperFileView2 mSuperFileView;
    String mFilePath;
    private String TAG = "FileDisplayActivity";
    private AppCompatImageView mIvBack;
    private AppCompatImageView mIvShare;
    private TextView mTvTitle;

    public static void show(Context context, String url) {
        Intent intent = new Intent(context, FileDisplayActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("path", url);
        intent.putExtras(bundle);
        context.startActivity(intent);

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    public void init() {
        mSuperFileView = (SuperFileView2) findViewById(R.id.mSuperFileView);
        mIvBack = (AppCompatImageView) findView(R.id.iv_include_top_bar_back);
        mIvShare = (AppCompatImageView) findView(R.id.iv_include_top_bar_share);
        mTvTitle = (TextView) findViewById(R.id.tv_include_top_bar_title);
        mSuperFileView.setOnGetFilePathListener(new SuperFileView2.OnGetFilePathListener() {
            @Override
            public void onGetFilePath(SuperFileView2 mSuperFileView2) {
                getFilePathAndShowFile(mSuperFileView2);
            }
        });
        mIvBack.setOnClickListener(this);
        mIvShare.setOnClickListener(this);

        Intent intent = this.getIntent();
        String path = (String) intent.getSerializableExtra("path");

        if (!TextUtils.isEmpty(path)) {
            Log.d(TAG, "文件path:" + path);
            mTvTitle.setText(new File(path).getName());
            setmFilePath(path);
        }
        mSuperFileView.show();
        changeStatusBarColorDark(FileDisplayActivity.this, R.color.white);
    }

    private void getFilePathAndShowFile(SuperFileView2 mSuperFileView2) {
        mSuperFileView2.displayFile(new File(getmFilePath()));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "FileDisplayActivity-->onDestroy");
        if (mSuperFileView != null) {
            mSuperFileView.onStopDisplay();
        }
    }

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_file_display;
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {

    }

    private String getmFilePath() {
        return mFilePath;
    }

    public void setmFilePath(String fileUrl) {
        this.mFilePath = fileUrl;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_include_top_bar_back:
                finish();
                break;
            case R.id.iv_include_top_bar_share:
                //通过FileProvider创建一个content类型的Uri
                File file = new File(mFilePath);
                Uri pdfUri = FileProvider.getUriForFile(FileDisplayActivity.this, AUTHORITIES, file);
                Intent intent = new Intent();
                //添加这一句表示对目标应用临时授权该Uri所代表的文件
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.addCategory("android.intent.category.DEFAULT");
                intent.setAction(Intent.ACTION_SEND);
                intent.putExtra(Intent.EXTRA_STREAM, pdfUri);
                intent.setType("application/pdf");
                try {
                    startActivity(Intent.createChooser(intent, file.getName()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
